package com.glwan.fixedassetapp.ui.setting;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.glwan.fixedassetapp.R;
import com.glwan.fixedassetapp.common.DatabaseConstant;
import com.glwan.fixedassetapp.ui.setting.activity.RFIDSettingActivity;
import com.glwan.fixedassetapp.utils.CommonUtil;
import com.glwan.fixedassetapp.utils.DatabaseUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SettingFragment extends Fragment {

    private SettingViewModel settingViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
//        settingViewModel =
//                ViewModelProviders.of(this).get(SettingViewModel.class);
//        final TextView textView = root.findViewById(R.id.text_setting);
//        settingViewModel.getText().observe(this, new Observer<String>() {
//            @Override
//            public void onChanged(@Nullable String s) {
//                textView.setText(s);
//            }
//        });
        View root = inflater.inflate(R.layout.fragment_setting, container, false);
        ListView listView = root.findViewById(R.id.settingsList);
        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), getData(), R.layout.setting_item, new String[]{"title"}, new int[]{R.id.tvSettingsItem});
        listView.setAdapter(simpleAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Context context = view.getContext();
                Map<String, Object> titleMap = (Map<String, Object>) parent.getAdapter().getItem(position);
                String title = titleMap.get("title").toString();
                if ((context.getString(R.string.clearAllData)).equals(title)) {
                    new AlertDialog.Builder(context).setIcon(R.drawable.warning_red).setTitle(R.string.tips).setMessage(getString(R.string.clearAllDataConfrim) + "?")
                            .setPositiveButton(getString(R.string.confirm), (dialog, which) -> {
                                // 点击“确认”后的操作
                                try {

                                    //TODO 发改委版本
                                    DatabaseUtil.deleteAllData(DatabaseConstant.INVENTORY_TASK);
                                    DatabaseUtil.deleteAllData(DatabaseConstant.INVENTORY_TASK_ITEM);
                                    DatabaseUtil.deleteAllData(DatabaseConstant.OFFLINE_INVENTORY_TASK_ITEM);

//                                    SQLUtil.clearALLSQL(CommonConstant.BASIC_PRODUCT_INFO, context);
//                                    SQLUtil.clearALLSQL(CommonConstant.INVENTORY_RECORD, context);
//
//                                    DatabaseUtil.execSQL(DatabaseConstant.RESET_SQLITE_SEQUENCE);

                                    CommonUtil.openAlertDialog(getFragmentActivity(), getString(R.string.success), getString(R.string.operationSuccess), R.drawable.success);


                                } catch (Exception e) {
                                    e.printStackTrace();
                                    CommonUtil.openAlertDialog(getFragmentActivity(), getString(R.string.error), getString(R.string.operationFail), R.drawable.error);

                                }
                            })
                            .setNegativeButton(getString(R.string.cancel), (dialog, which) -> {
                                // 点击“取消”后的操作,这里不设置没有任何操作
                            }).show();
                }
//                if(getString(R.string.dropAllTable).equals(title)){
//                    new AlertDialog.Builder(context).setTitle(getString(R.string.clearAllDataConfrim) + "?")
//                            .setPositiveButton(getString(R.string.confirm), (dialog, which) -> {
//                                // 点击“确认”后的操作
//                                try{
////                                    SQLUtil.dropTable(CommonConstant.BASIC_PRODUCT_INFO,context);
////                                    SQLUtil.dropTable(CommonConstant.INVENTORY_RECORD,context);
//                                    boolean b = context.deleteDatabase(CommonConstant.DATABASE_NAME);
//                                    Log.i("deleteDatabase", String.valueOf(b));
//                                }
//                                catch(Exception e ){
//                                    e.printStackTrace();
//                                }
////                                new DatabaseHelper(context).onCreate(CommonConstant.DATABASE_NAME);
//                                CommonUtil.openAlertDialog(context,getString(R.string.confirm),getString(R.string.operationSuccess));
//                            })
//                            .setNegativeButton(getString(R.string.cancel), (dialog, which) -> {
//                                // 点击“取消”后的操作,这里不设置没有任何操作
//                            }).show();
//                }
//                context.getString(R.string.logoutConfirm)
//                else if((context.getString(R.string.dropAllTable)).equals(title)){
//                    new AlertDialog.Builder(context).setTitle(getString(R.string.deleteDataTablesConfirm) + "?")
//                            .setPositiveButton(getString(R.string.confirm), (dialog, which) -> {
//                                // 点击“确认”后的操作
//                                SQLUtil.dropTable(CommonConstant.BASIC_PRODUCT_INFO,context);
//                                CommonUtil.openAlertDialog(context,getString(R.string.confirm),getString(R.string.operationSuccess));
//                            })
//                            .setNegativeButton(getString(R.string.cancel), (dialog, which) -> {
//                                // 点击“取消”后的操作,这里不设置没有任何操作
//                            }).show();
//
//                }
//                else if("导出已盘表".equals(title)){
//                    new AlertDialog.Builder(context).setTitle("是否导出已盘表?")
//                            .setPositiveButton(getString(R.string.confirm), (dialog, which) -> {
//                                // 点击“确认”后的操作
//                                try {
//                                    POIHelper.exportCheckProductCodeCsv(context);
//                                } catch (ParseException e) {
//                                    e.printStackTrace();
//                                }
////                                CommonUtil.openAlertDialog(context,getString(R.string.confirm),getString(R.string.operationSuccess));
//                            })
//                            .setNegativeButton(getString(R.string.cancel), (dialog, which) -> {
//                                // 点击“取消”后的操作,这里不设置没有任何操作
//                            }).show();
//
//                }
                else if ((context.getString(R.string.RFIDSetting)).equals(title)) {
                    CommonUtil.startActivity(context, RFIDSettingActivity.class, null, false);
                }
//                else if((context.getString(R.string.systemConfig)).equals(title)){
//                    CommonUtil.startActivity(context, SystemSettingActivity.class,false);
//
//                }
            }
        });


//        listView.setOnItemClickListener((adapterView, view, i, l) -> {
//
//
//            View childLayout = view.findViewById(R.id.childLayout);
//            if(childLayout == null){
//                return;
//            }
//            TextView textView = view.findViewById(R.id.itemStatus);
//            if(childLayout.getVisibility() != View.GONE ){
//                childLayout.setVisibility(View.GONE);
//            }
//            else {
//                childLayout.setVisibility(View.VISIBLE);
//            }
//
//        });

        return root;
    }

    private List<Map<String, Object>> getData() {
        String[] titles = {getString(R.string.clearAllData), getString(R.string.RFIDSetting)};

        List<Map<String, Object>> list = new ArrayList<>();
        for (String title : titles) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", title);
            list.add(map);
        }
        return list;
    }

    private Activity getFragmentActivity() {
        return this.getActivity();
    }
}